<?php
require 'Model.php';

	class MusicModel extends Model{
		function __Construct(){
		parent::__Construct();
	}
	function getmusicList(){
		
		$sql = "select * from musics";
		$data = $this->conn->query($sql);

		return $data;
	}
	function delmusic($mid){


	$sql = "select * from musics where mid=$mid";
	$result=mysql_query($sql);
	$row=mysql_fetch_assoc($result);
	$filename='./photos/'.$row["pic"];
	//echo $filename;
	//删除数据库相应信息，如果删除成功，删除该视频的海报文件
	$sql = "delete from musics where mid=$mid";
	$result = mysql_query($sql) or die('删除失败！'.mysql_error());
	if(!mysql_error()){
		//如果有海报，则删除海报文件
		if($filename!='./photos/' && file_exists($filename))
			unlink($filename);
			return $result;
			/*$sql="delete from users where uid=$uid";
			$result=$this->conn->query($sql);
			return $result;*/

	}
		}
	
	

	function addmusic($data,$file){
	
        $newname = $this->uploadfile($file);//调用uploadfile方法，得到newname

        $musicname=$data['musicname'];
        $uploaddate=$data['uploaddate'];
        $tid=$data['tid'];
        $intro=$data['intro'];
        $pic=$newname;
        $address=$data['address'];
        $uploadadmin=$data['uploadadmin'];
        $sql="insert into musics VALUES ('null','$musicname','$tid','$pic','$intro','$uploaddate','uploadadmin','null','null','$address')";
        $result=$this->conn->query($sql);
        return $result;
    }
	
	function editmusic($mid){
		$sql = "select * from musics where mid=$mid";
		//echo $sql;
		$data=$this->conn->query($sql);
		return $data;
		//var_dump($row);
		/*$uname = $row['uname'];
		$password = $row['upassword'];
		$gender = $row['gender'];
		$birthdate = $row['birthdate'];
		$hobby = $row['hobby'];
		$degree = $row['degree'];
		$intro = $row['intro'];
		$filepath = './photos/';
		$pic = UserPhotoPath.$row['pic'];
*/}
		function updatemusic($data,$file){
			//接受数据
				$mid=$data['mid'];
				$mideoname=$data['musicname'];
       			$uploaddate=$data['uploaddate'];
        		$tid=$data['tid'];
      		    $intro=$data['intro'];
        		$pic=$newname;
        		$address=$data['address'];
        		$uploadadmin=$data['uploadadmin'];
				$oldpic=$data['oldpic'];
			//如果修改了头像，需要上传新头像，否则原头像不动
				if ($file['pic']['size']==0) {
					$sql="update musics set musicname='$musicname',uploaddate='$uploaddate',tid=$tid,address='$address',uploadadmin='$uploadadmin',intro='$intro' where mid=$mid";
					//整型不用写单引号 degree=$degree
				}
				else{
					//删除原来的头像
					if($filename!='./photos/' && file_exists($filename))
					{$filename='./photos/'.$oldpic;
					unlink($filename);}
					//调用方法上传图片
					$newname = $this->uploadfile($file);//调用uploadfile方法，得到newname

					//上传新头像并且根据uid更新数据
					/*$arr=explode('.', $file['pic']['name']);
     			   	$suffix=$arr[count($arr)-1];
       				$newname=date("YmdHis").rand(100,999).'.'.$suffix;
        			$filepath = './photos/';
        			move_uploaded_file($_FILES['pic']['tmp_name'], $filepath.$newname);*///上传文件到指定文件夹，tmp_name是file里带的
        			$sql="update musics set musicname='$mideoname',uploaddate='$uploaddate',tid=$tid,address='$address',uploadadmin='$uploadadmin',intro='$intro',pic='$newname' where mid=$mid";
				}
					//echo $sql;
				   $result=$this->conn->query($sql);
        			return $result;

			//根据uid修改数据库中的数据

		}
}

  ?>
		
